package com.yjhb.android.sbz.activity;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.rule.RuleBiddingActivity;
import com.yjhb.android.sbz.adapter.BiddingAdapter;
import com.yjhb.android.sbz.entity.BiddingUser;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.Task;
import com.yjhb.android.sbz.mvp.bidding.BiddingListPresenter;
import com.yjhb.android.sbz.mvp.bidding.BiddingView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.CircleImageView;
import com.jojo.android.zxlib.view.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/8 14:41
 */
public class BiddingActivity extends BaseActivity<BiddingListPresenter> implements BiddingView {
    private final String TAG = "BiddingActivity";

    private final static int REQUEST_OLD_INFO = 10021;

    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;
    @BindView(R.id.participate_tv)
    TextView participate_tv;
    @BindView(R.id.my_head_iv)
    CircleImageView myHeadIv;
    @BindView(R.id.nickname_tv)
    TextView nicknameTv;
    @BindView(R.id.bid_tv)
    TextView bidTv;
    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.label1)
    TextView label1;
    @BindView(R.id.label2)
    TextView label2;
    @BindView(R.id.myBidding_layout)
    CardView myBiddingLayout;
    @BindView(R.id.my_old_head_iv)
    CircleImageView myOldHeadIv;
    @BindView(R.id.old_nickname_tv)
    TextView oldNicknameTv;
    @BindView(R.id.old_bid_tv)
    TextView oldBidTv;
    @BindView(R.id.old_time_tv)
    TextView oldTimeTv;
    @BindView(R.id.old_label1)
    TextView oldLabel1;
    @BindView(R.id.old_label2)
    TextView oldLabel2;
    @BindView(R.id.old_label_layout)
    LinearLayout oldLabelLayout;
    @BindView(R.id.myOldBidding_layout)
    CardView myOldBiddingLayout;
    @BindView(R.id.replacementTask_tv)
    TextView replacementTask_tv;


    BiddingAdapter biddingAdapter;
    List<BiddingUser> users = new ArrayList<>();
    BiddingUser myBidding;  //我的今日竞价信息
    BiddingUser myOldBidding; //我的昨日竞价信息

    @Override
    protected BiddingListPresenter initPresenter() {
        return new BiddingListPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bidding;
    }

    @Override
    protected void initUI() {
        recyclerView.setEmptyViewById(R.id.layout_empty);
        recyclerView.setAdapter(biddingAdapter = new BiddingAdapter(this));
        biddingAdapter.setData(users);
    }

    @Override
    protected void initData() {
        refreshMyInfo();
    }

    @Override
    protected void regEvent() {
    }

    @Override
    public void refresh() {
        loadingDialog.show();
        presenter.getBidList();
        presenter.getMyBidding();
        presenter.getYesterdayBidding();
    }

    @Override
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick(R.id.title_right_tv)
    void title_right_tv() {
        startActivity(new Intent(this, RuleBiddingActivity.class));
    }

    @OnClick(R.id.more_tv)
    void more_tv() {
        startActivity(new Intent(this, BiddingMoreActivity.class));
    }

    @OnClick(R.id.participate_layout)
    void participate_layout() {
        Intent intent = new Intent(this, PutBiddingActivity.class);
        intent.putExtra("myinfo", myBidding);
        startActivity(intent);
    }

    private void refreshMyInfo() {
        try {
            if (myBidding != null) {
                myBiddingLayout.setVisibility(View.VISIBLE);
                Glide.with(this).load(myBidding.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(myHeadIv);
                timeTv.setText("竞价时间:" + ZxTimeTool.getTimeStr(myBidding.getCreateTime()));
                bidTv.setText("¥" + myBidding.getMoney());
                label1.setText(Tool.idTransformString(myBidding.getCategoryId()));
                label2.setText(myBidding.getName());
            } else {
                myBiddingLayout.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }

    }

    /**
     * 更细我的昨日的
     */
    private void refreshOldMyInfo() {
        try {
            if (myOldBidding != null) {
                myOldBiddingLayout.setVisibility(View.VISIBLE);
                Glide.with(this).load(myOldBidding.getAvatar()).apply(new RequestOptions().placeholder(R.mipmap.test_head)).into(myOldHeadIv);
                oldTimeTv.setText("竞价时间:" + ZxTimeTool.getTimeStr(myOldBidding.getCreateTime()));
                oldBidTv.setText("¥" + myOldBidding.getMoney());
                oldLabel1.setText(Tool.idTransformString(myOldBidding.getCategoryId()));
                oldLabel2.setText(myOldBidding.getName());
            } else {
                myOldBiddingLayout.setVisibility(View.GONE);
            }
        } catch (Exception e) {
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OLD_INFO && resultCode == RESULT_OK) {
            Task task = (Task) data.getSerializableExtra("selectedTask");
            this.myOldBidding.setName(task.getName());
            presenter.adsChange(task.getId());
        }
    }

    @OnClick(R.id.replacementTask_tv)
    void replacementTask_tv() {
        Intent intent = new Intent(this, BiddingTaskListActivity.class);
        startActivityForResult(intent, REQUEST_OLD_INFO);
    }


    @Override
    public void onWait() {

    }

    @Override
    public void onSuccess(List<BiddingUser> data) {
        loadingDialog.cancel();
        if (data != null && data.size() > 0) {
            this.users.clear();
            this.users.addAll(data);
        }
        biddingAdapter.notifyDataSetChanged();
        refreshMyInfo();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        biddingAdapter.notifyDataSetChanged();
//        refreshMyInfo();
        if (code != -1)
            ZxToast.showINFOBottomToast(msg);
    }


    @Override
    public void onMyBidding(BiddingUser biddingUser) {
        myBidding = biddingUser;
        if (myBidding != null) {
            participate_tv.setText("继续加价");
        } else {
            participate_tv.setText("参与竞价");
        }
        refreshMyInfo();
    }

    @Override
    public void onMyOldBidding(BiddingUser biddingUser) {
        myOldBidding = biddingUser;
        refreshOldMyInfo();
    }

    @Override
    public void onChangeTask() {
        ZxToast.showSuccessBottomToast("更改成功");
        refreshOldMyInfo();
    }
}
